import 'dart:math';

import 'package:flutter/material.dart';

class PublicColor {
  /// 全局色
  static Color primary = const Color(0xFFFFAA00);
  static Color lightPrimary = const Color(0xFFFFC72B);

  /// 黑色字体
  static Color darkText = const Color(0xFF333333);

  /// 灰色字体
  static Color grayText = const Color(0xFF666666);

  /// 浅色字体
  static Color lightText = const Color(0xFF999999);

  /// 占位符
  static Color hintText = const Color(0xFFCCCCCC);

  /// 边框
  static Color border = const Color(0xFFDDDDDD);

  /// 分割线/浅色边框
  static Color divider = const Color(0xFFE5E5E5);

  /// 背景
  static Color background = const Color(0xFFF8F8F8);

  /// appBar
  static Color appBar = Colors.white;
  static Color lightAppBar = Colors.white;

  /// 阴影
  static Color shadow = const Color(0x16000000);

  /// 蒙层
  static Color mask = const Color(0x80000000);

  /// 随机色
  static get random => Color.fromARGB(
        255,
        Random().nextInt(255),
        Random().nextInt(255),
        Random().nextInt(255),
      );

  /// 色值
  static Color? hex(String? hex, {double? alpha}) {
    if (hex == null || hex.length < 6) {
      return null;
    }
    String colorStr = hex;

    if (colorStr.length == 6) {
      // 000000
      colorStr = '0xff$colorStr';
    } else if (colorStr.startsWith('#') && colorStr.length == 7) {
      // #000000
      colorStr = colorStr.replaceRange(0, 1, '0xff');
    } else if (colorStr.length == 8) {
      // 0x000000 FF000000
      if (colorStr.toLowerCase().startsWith('0x')) {
        colorStr = colorStr.replaceRange(0, 2, '0xff');
      } else {
        colorStr = '0x$colorStr';
      }
    }
    // 0xff000000
    Color color = Color(int.parse(colorStr));
    int red = color.red;
    int green = color.green;
    int blue = color.blue;
    final a = color.alpha / 255.0;
    return Color.fromRGBO(red, green, blue, alpha ?? a);
  }
}
